﻿package com.cw.war.ui 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 * 服务器时间
	 * @author DanielLu
	 */
	public final class ServerTimer extends EventDispatcher
	{
		private static var instance : ServerTimer;

		public static function getInstance () : ServerTimer
		{
			if(instance == null)instance = new ServerTimer( );
			return instance;
		}

		private var date : Date;

		/**
		 * 服务器时间
		 */
		public function ServerTimer ()
		{
			super( );
			this.date = new Date( );
		}

		/**
		 * 设置当前的时间
		 * @param time		"2009-12-13 23:59:58"
		 */
		public function setTime (timeString : String) : void
		{
			if(!timeString)
			{
			    return;
			}
			Time.getInstance( ).removeTimerListener( this.__onTimer );
			//
			//格式化时间字符串
			var timeArr : Array = timeString.split( " " );
			var yearArr : Array = timeArr[0].split( "-" );
			var year : Number = yearArr[0];
			var month : Number = yearArr[1];
			var day : Number = yearArr[2];

			var hourArr : Array = timeArr[1].split( ":" );
			var hour : Number = hourArr[0];
			var minute : Number = hourArr[1];
			var second : Number = hourArr[2];

			//解析服务端字符串
			this.date.setFullYear( year , month - 1 , day );
			this.date.setHours( hour , minute , second );
			//
			Time.getInstance( ).addTimerListener( this.__onTimer );
		}

		/**
		 * 返回时间字符串
		 * @return		时间字符串
		 */
		public function getCurrentTime () : Date
		{
			//var timeString : String = "";
			//timeString = LocaleManager.getString( "ServerTime.date" , this.date.fullYear , this.date.month , this.date.day , this.date.hours , this.date.minutes , this.date.seconds );
			//timeString += " " + LocaleManager.getString( "ServerTime.hour" + this.date.hours );
			return this.date;
		}

		private function __onTimer (e : Event) : void
		{
			//往后走一秒
			this.date.setSeconds( this.date.getSeconds( ) + 1 );
			this.fireChange( );
		}

		public static function formatTimeString (timeString : String) : Date
		{
			var date : Date = new Date( );
			var timeArr : Array = timeString.split( " " );
			var yearArr : Array = timeArr[0].split( "-" );
			var year : Number = yearArr[0];
			var month : Number = yearArr[1];
			var day : Number = yearArr[2];

			var hourArr : Array = timeArr[1].split( ":" );
			var hour : Number = hourArr[0];
			var minute : Number = hourArr[1];
			var second : Number = hourArr[2];

			//解析服务端字符串
			date.setFullYear( year , month - 1 , day );
			date.setHours( hour , minute , second );
			return date;
		}

		//---------------------------------------------------------------------------------
		/**
		 * 添加跑秒侦听器。
		 * @param listener			侦听器
		 * @param priority			优先级
		 * @param useWeakReference	弱引用
		 */
		public function addChangeListener (listener : Function, priority : int = 0, useWeakReference : Boolean = false) : void
		{
			this.addEventListener( "change" , listener , false , priority , useWeakReference );
		}

		/**
		 * 删除跑秒侦听器。
		 * @param listener			侦听器。
		 */
		public function removeChangeListener (listener : Function) : void
		{
			this.removeEventListener( "change" , listener , false );
		}

		/**
		 * 发送正在倒计时的事件。
		 */
		private function fireChange () : void
		{	
			this.dispatchEvent( new Event( "change" ) );
		}
	}
}
